
package com.yuke.cloud.service.uac.manager;

import com.google.common.base.Preconditions;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.config.YukeCoreConfig;
import com.yuke.cloud.common.core.config.RedisConfiguration;
import com.yuke.cloud.common.core.mq.RabbitMetaMessage;
import com.yuke.cloud.common.core.mq.RabbitSender;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.uac.dao.AccUserGroupMapper;
import com.yuke.cloud.service.uac.dao.AccUserMapper;
import com.yuke.cloud.service.uac.dao.AccUserRoleMapper;
import com.yuke.cloud.service.uac.entity.AccUser;
import com.yuke.cloud.service.uac.entity.AccUserGroup;
import com.yuke.cloud.service.uac.entity.AccUserRole;
import com.yuke.cloud.service.uac.service.AccUserGroupService;
import com.yuke.cloud.service.uac.service.AccUserRoleService;
import com.yuke.cloud.service.uac.service.impl.RedisServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * The class User manager.
 *
 * @author
 */
@Slf4j
@Component
//@ComponentScan(basePackageClasses={RabbitSender.class, RedisConfiguration.class, YukeCoreConfig.class})  //todo 用于rabbitmq相关
@Import(RabbitSender.class)
@Transactional(rollbackFor = Exception.class)
public class UserManager {
	@Resource
	private AccUserMapper accUserMapper;
	@Resource
	private AccUserRoleService accUserRoleService;
	@Resource
	private AccUserGroupService accUserGroupService;
	@Resource
	private RedisServiceImpl redisService;

	@Autowired
	RabbitSender rabbitSender;

	/** 发消息给opc模块 */
	public void submitResetPwdEmail(final RabbitMetaMessage messageData) throws Exception {
		log.info("重置密码发送邮件. messageData={}", messageData);
		rabbitSender.send(messageData);
	}

	/** 发消息给opc模块 */
	public void register(final RabbitMetaMessage mqMessageData, final AccUser accUser) throws Exception {
		log.info("注册用户. mqMessageData={}, user={}", mqMessageData, accUser);
		rabbitSender.send(mqMessageData);
		accUserMapper.insertSelective(accUser);
	}

	/** 发消息给opc模块 */
	public void resetLoginPwd(final RabbitMetaMessage mqMessageData, final AccUser update) throws Exception {
		log.info("重置密码. mqMessageData={}, user={}", mqMessageData, update);
		rabbitSender.send(mqMessageData);
		int updateResult = accUserMapper.updateByPrimaryKeySelective(update);
		if (updateResult < 1) {
			log.error("用户【 {} 】重置密码失败", update.getLoginName());
		} else {
			log.info("用户【 {} 】重置密码失败", update.getLoginName());
		}
	}

	/* 目前注册用户不需要激活，因此该方法可能用不到，在注册的时候已经把用户的默认角色和组织进行了绑定 */
	/** 发消息给opc模块 */
	public void activeUser(final RabbitMetaMessage mqMessageData, final AccUser accUser, final String activeUserKey) throws Exception {
		log.info("激活用户. mqMessageData={}, user={}", mqMessageData, accUser);
		rabbitSender.send(mqMessageData);
		int result = accUserMapper.updateByPrimaryKeySelective(accUser);
		if (result < 1) {
			throw new UacBizException(ErrorCodeEnum.UAC10011038, accUser.getUserId());
		}

		// 绑定一个访客角色默认值roleId=10000
		final Long userId = accUser.getUserId();
		Preconditions.checkArgument(userId != null, "用戶Id不能爲空");

		final Long roleId = GlobalConstant.Sys.CONSUMER_ROLE_ID; //10000L;  // todo 默认值应设置为普通用户

		AccUserRole userRole = new AccUserRole();
		userRole.setUserId(userId);
		userRole.setRoleId(roleId);
//		accUserRoleMapper.insertSelective(userRole);
		userRole.setUserRoleId(accUserRoleService.generateUserRoleId());
		accUserRoleService.save(userRole);
		// 绑定一个组织
		AccUserGroup userGroup = new AccUserGroup();
		userGroup.setUserId(userId);
		userGroup.setGroupId(GlobalConstant.Sys.GROUP_CUSTOMER/*GlobalConstant.Sys.SUPER_MANAGER_GROUP_ID*/);  // todo 对于注册的新用户，因为默认角色为普通用户，不绑定组织
//		accUserGroupMapper.insertSelective(userGroup);
		userGroup.setUserGroupId(accUserGroupService.generateUserGroupId());
		accUserGroupService.save(userGroup);
		// 删除 activeUserToken
		redisService.deleteKey(activeUserKey);
	}

	/** 发消息给opc模块 */
	public void sendSmsCode(final RabbitMetaMessage mqMessageData) throws Exception {
		log.info(". mqMessageData={}", mqMessageData);
		rabbitSender.send(mqMessageData);
	}

	/** 发消息给opc模块 */
	public void sendEmailCode(final RabbitMetaMessage mqMessageData) throws Exception {
		log.info("发送邮件验证码. mqMessageData={}", mqMessageData);
		rabbitSender.send(mqMessageData);
	}
}
